// import { OverlayConstant } from './OverlayConstant'
import { Overlay } from 'ol'

export class Ovelayer {
//   public overlayInstance: ol.Overlay
//   public overlayData: any
//   public overlayPosition: Array<any>
//   public overlayEventType: string
//   public overlayEventFunArray: Array<any>
//   public config: MapConfig
//   public overlayTools: OverlayTools

  constructor(overlayConfig, element) {
    this.overlayData = overlayConfig
    const position_x = overlayConfig.geometry.points[0].x
    const position_y = overlayConfig.geometry.points[0].y
    const positionPoint = [position_x, position_y]
    this.overlayPosition = positionPoint.slice(0)
    this.overlayInstance = new Overlay({
      id: overlayConfig.id ? overlayConfig.id : undefined,
      element: element ? element : undefined,
      position: positionPoint ? positionPoint : undefined,
      insertFirst: overlayConfig.hasOwnProperty('insertFirst')
        ? overlayConfig.insertFirst
        : true,
      stopEvent: true,
    })
    // this.overlayTools = OverlayTools.getInstance()
  }

  // 获取当前覆盖物
   getOverlay() {
    return this.overlayInstance
  }

  // 获取覆盖物的位置信息
   getOverlayPosition() {
    return this.overlayInstance.getPosition()
  }

  // 获取覆盖物的dom元素
   getOverlayElement() {
    return this.overlayInstance.getElement()
  }

  // 获取覆盖物的Id
   getOverlayId() {
    return this.overlayInstance.getId()
  }

  // 创建事件函数
   createFun(
    data,
    type,
    position,
    callback,
  ) {
    this.overlayEventFunArray
      ? this.overlayEventFunArray
      : (this.overlayEventFunArray = [])
    let createFun = function (event) {
      event.stopPropagation()
      const eventData = {}
      eventData['data'] = data
      eventData['type'] = type
      eventData['target'] = event.target
      eventData['position'] = position
      callback(eventData)
    }
    this.overlayEventFunArray.push({ callback: callback, func: createFun })
  }

  // 启动覆盖物的事件监听
 on(type, callback) {
    const eventEle = this.getOverlayElement()
    this.createFun(this.overlayData, type, this.overlayPosition, callback)
    let len = this.overlayEventFunArray.length
    for (let i = 0; i < len; i++) {
      if (this.overlayEventFunArray[i].callback == callback) {
        eventEle.addEventListener(
          type,
          this.overlayEventFunArray[i].func,
          false
        )
      }
    }
  }

  // 移除覆盖物的事件监听
   un(type, callback) {
    const eventEle = this.getOverlayElement()
    let funIndex
    let len = this.overlayEventFunArray.length
    for (let i = 0; i < len; i++) {
      if (this.overlayEventFunArray[i].callback == callback) {
        eventEle.removeEventListener(
          type,
          this.overlayEventFunArray[i].func,
          false
        )
        funIndex = i
      }
    }
    this.overlayEventFunArray.splice(funIndex, 1)
  }

  // 居中当前覆盖物
//    setCenter() {
//     const kspmap = this.overlayTools.getKspmap()
//     kspmap.setMapCenterAndZoom(
//       this.overlayPosition,
//       OverlayConstant.DEFAULT_MARKER_ZOOM
//     )
//   }
}
